package com.extremelylargepackage.editor;

import java.util.ArrayList;
import java.util.Stack;

import com.extremelylargepackage.R;
import com.extremelylargepackage.level.CommandEvent;
import com.extremelylargepackage.level.Level;
import com.extremelylargepackage.level.LevelEvent;
import com.extremelylargepackage.level.Wave;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class WavesEditor extends ActivityForcePortrait 
{
	private Level _level;
	private ListView _list;
	private ArrayAdapter<LevelEventItem> _adapter;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		_level = (Level) getIntent().getExtras().get("level");
		
		this.setContentView(R.layout.waves_editor);
		_list = (ListView) this.findViewById(R.id.eventList);
		constructList();
		_list.setOnItemClickListener(new WaveClickHandler());
		_list.setOnItemLongClickListener(new WaveLongClickHandler());
		Button addWave = (Button) findViewById(R.id.newWave);
		Button ret = (Button) findViewById(R.id.waves_editor_return);
		ret.setOnClickListener(new ReturnHandler());
		addWave.setOnClickListener(new AddWaveHandler());
	}
	
	@Override
	public void onBackPressed()
	{
		setResultAndFinish();
	}
	
	public void setResultAndFinish()
	{
		Intent i = new Intent();
		i.putExtra("level", _level);
		setResult(Activity.RESULT_OK, i);
		finish();
	}
	
	private void constructList()
	{
		ArrayList<LevelEventItem> sList = new ArrayList<LevelEventItem>();
		LevelEvent[] eArray = new LevelEvent[_level.getEventQueue().size()];
		_level.getEventQueue().toArray(eArray);
		//First wave is the dummy wave, dont display it.
		for(int i = 1; i < eArray.length; i++)
		{
			LevelEventItem item = new LevelEventItem(eArray[i], i);
			sList.add(item);
		}
		
		_adapter = new ArrayAdapter<LevelEventItem>(this, android.R.layout.simple_list_item_1, sList);
		_list.setAdapter(_adapter);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		switch(resultCode)
		{
			case Activity.RESULT_OK:
				Wave w = (Wave) data.getExtras().get("wave");
				int num = data.getExtras().getInt("num");
				((LevelEventItem)_list.getItemAtPosition(num - 1)).setLevelEvent(w);
				_adapter.notifyDataSetChanged();
				break;
			default:
				break;
		}
	}
	
	private class AddWaveHandler implements OnClickListener
	{
		@Override
		public void onClick(View v) 
		{
			_level.getEventQueue().add(new Wave());
			constructList();
		}
	}
	
	private class ReturnHandler implements OnClickListener
	{
		@Override
		public void onClick(View v) 
		{
			setResultAndFinish();
		}
	}
	
	private class LevelEventItem
	{
		private LevelEvent _event;
		private boolean _isWave;
		private boolean _isCommand;
		private int _num;
		
		public LevelEventItem(LevelEvent event, int num)
		{
			_event = event;
			_isWave = event instanceof Wave;
			_isCommand = event instanceof CommandEvent; 
			_num = num;
		}
		
		public void edit()
		{
			if(_isWave)
			{
				Intent intent = new Intent(WavesEditor.this, WaveEditor.class);
				intent.putExtra("wave", _event);
				intent.putExtra("num", _num);
				startActivityForResult(intent, 0);
			}
			else
			{
				//Do nothing.
			}
		}
		
		public void setLevelEvent(LevelEvent event)
		{
			_event = event;
			_level.replaceEvent(_num, event);
		}
		
		@Override
		public String toString()
		{
			if(_isWave)
			{
				Wave wave = (Wave) _event;
				return getEventName() + ", " + wave.getEnemies().size() + " enemies";
			}
			else if(_isCommand)
			{
				return getEventName();
			}
			return getEventName();
		}	
		
		public String getEventName()
		{
			if(_isWave)
			{
				return "Wave " + _num;
			}
			else if(_isCommand)
			{
				return "Command Event " + _num;
			}
			return "ERROR - Unknown LevelEvent";
		}
		
		public LevelEvent getEvent()
		{
			return _event;
		}
	}
	
	private class WaveClickHandler implements OnItemClickListener
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
		{
			((LevelEventItem) _list.getItemAtPosition(position)).edit();
		}		
	}
	
	private class WaveLongClickHandler implements OnItemLongClickListener
	{
		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) 
		{
			final LevelEventItem lei = (LevelEventItem) _list.getItemAtPosition(position);
			new AlertDialog.Builder(WavesEditor.this).setCancelable(true).setMessage("Delete " + lei.getEventName() + " ?")
			.setPositiveButton("Yes",  new DialogInterface.OnClickListener() 
			{
				@Override
				public void onClick(DialogInterface dialog, int which) 
				{
					_level.getEventQueue().remove(lei.getEvent());
					constructList();
				}
			}).setNegativeButton("No", null).show();
			return true;
		}
	}
}
